<template>
  <div class="check-in-record">
    <!-- 查询条件 -->
    <div class="query-section">
      <el-form :model="queryForm" inline>
        <el-form-item label="日期范围">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="打卡计划">
          <el-select v-model="queryForm.planId" placeholder="选择打卡计划" clearable>
            <el-option
              v-for="plan in plans"
              :key="plan.id"
              :label="plan.title"
              :value="plan.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchRecords">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 记录列表 - 一行一条卡片布局 -->
    <div class="record-cards" v-loading="loading">
      <el-empty v-if="records.length === 0" description="暂无打卡记录"></el-empty>
      
      <el-row :gutter="20">
        <el-col :span="24" v-for="record in records" :key="record.id" class="card-col">
          <el-card shadow="hover" class="record-card">
            <div class="card-content">
              <div class="plan-title">{{ record.planTitle }}</div>
              
              <div class="card-left">
                <div class="plan-icon" :class="'plan-type-' + record.planType">
                  <el-icon v-if="record.planType === 'daily'"><Calendar /></el-icon>
                  <el-icon v-else-if="record.planType === 'weekly'"><Timer /></el-icon>
                  <el-icon v-else><Aim /></el-icon>
                </div>
              </div>
              
              <div class="card-middle">
                <div class="plan-info">
                  <el-tag :type="getTypeTag(record.planType)" size="small" class="plan-type-tag">
                    {{ getTypeText(record.planType) }}
                  </el-tag>
                  <span class="date-info">打卡日期: {{ record.date }}</span>
                  <span class="time-info">打卡时间: {{ record.checkInTime }}</span>
                </div>
                <div class="plan-remark" v-if="record.remark">
                  <div class="remark-label">备注:</div>
                  <div class="remark-content">{{ record.remark }}</div>
                </div>
              </div>
              
              <div class="card-right">
                <div class="status-tag">
                  <el-tag :type="record.status === 'completed' ? 'success' : 'danger'" size="small">
                    {{ record.status === 'completed' ? '已完成' : '未完成' }}
                  </el-tag>
                </div>
                <el-button type="primary" size="small" @click="viewDetail(record)" class="detail-btn">
                  查看详情
                </el-button>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          background
          layout="prev, pager, next, sizes, total"
          :total="total"
          :page-size="queryForm.pageSize"
          :current-page="queryForm.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 50]"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { Calendar, Timer, Aim } from '@element-plus/icons-vue'

export default {
  name: 'CheckInRecord',
  components: {
    Calendar,
    Timer,
    Aim
  },
  data() {
    return {
      loading: false,
      queryForm: {
        dateRange: [],
        planId: '',
        currentPage: 1,
        pageSize: 10
      },
      plans: [], // 打卡计划列表
      records: [], // 打卡记录列表
      total: 0 // 总记录数
    }
  },
  created() {
    this.getPlans()
    this.getRecords()
  },
  methods: {
    // 获取打卡计划列表
    getPlans() {
      // 这里应该调用API获取打卡计划列表
      // 模拟数据
      this.plans = [
        { id: 1, title: '早起打卡' },
        { id: 2, title: '健身打卡' },
        { id: 3, title: '阅读打卡' }
      ]
    },
    // 获取打卡记录
    getRecords() {
      this.loading = true
      // 这里应该调用API获取打卡记录
      // 模拟数据
      setTimeout(() => {
        this.records = [
          {
            id: 1,
            date: '2023-05-01',
            planId: 1,
            planTitle: '早起打卡',
            planType: 'daily',
            status: 'completed',
            checkInTime: '2023-05-01 06:30:00',
            remark: '今天起得很早，感觉很好'
          },
          {
            id: 2,
            date: '2023-05-02',
            planId: 1,
            planTitle: '早起打卡',
            planType: 'daily',
            status: 'completed',
            checkInTime: '2023-05-02 06:45:00',
            remark: '有点迟了，但还是起来了'
          },
          {
            id: 3,
            date: '2023-05-03',
            planId: 2,
            planTitle: '健身打卡',
            planType: 'weekly',
            status: 'completed',
            checkInTime: '2023-05-03 18:20:00',
            remark: '今天做了30分钟有氧运动'
          }
        ]
        this.total = 3
        this.loading = false
      }, 500)
    },
    // 查询记录
    searchRecords() {
      this.queryForm.currentPage = 1
      this.getRecords()
    },
    // 重置查询条件
    resetQuery() {
      this.queryForm = {
        dateRange: [],
        planId: '',
        currentPage: 1,
        pageSize: 10
      }
      this.getRecords()
    },
    // 查看详情
    viewDetail(row) {
      console.log('查看详情', row)
      // 这里可以跳转到详情页或者打开详情弹窗
    },
    // 处理每页显示条数变化
    handleSizeChange(size) {
      this.queryForm.pageSize = size
      this.getRecords()
    },
    // 处理页码变化
    handleCurrentChange(page) {
      this.queryForm.currentPage = page
      this.getRecords()
    },
    // 获取计划类型标签样式
    getTypeTag(type) {
      const typeMap = {
        daily: 'primary',
        weekly: 'success',
        monthly: 'warning'
      }
      return typeMap[type] || 'info'
    },
    // 获取计划类型文本
    getTypeText(type) {
      const typeMap = {
        daily: '每日打卡',
        weekly: '每周打卡',
        monthly: '每月打卡'
      }
      return typeMap[type] || '未知类型'
    }
  }
}
</script>

<style scoped>
.check-in-record {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.header {
  margin-bottom: 20px;
}

.header h1 {
  font-size: 24px;
  color: #303133;
  margin: 0;
  color: #2D3436;  /* 修改为深灰 */
}

.query-section {
  background-color: #f5f7fa;
  padding: 20px;
  border-radius: 4px;
  margin-bottom: 20px;
}

.record-list {
  background-color: #FFFFFF;  /* 确保为纯白 */
  padding: 20px;
  border-radius: 4px;
}
.record-cards {
  min-height: 300px;
}

.card-col {
  margin-bottom: 16px;
}

.record-card {
  transition: all 0.3s;
  border-radius: 8px;
  overflow: hidden;
}

.record-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}

.card-content {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.plan-title {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
  margin-right: 20px;
  min-width: 120px;
}

.card-left {
  margin-right: 20px;
}

.plan-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
}

.plan-type-daily {
  background-color: #409EFF;
}

.plan-type-weekly {
  background-color: #67C23A;
}

.plan-type-monthly {
  background-color: #E6A23C;
}

.card-middle {
  flex: 1;
}

.plan-title {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 8px;
}

.plan-info {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 8px;
  font-size: 14px;
  color: #606266;
}

.plan-type-tag {
  margin-right: 15px;
}

.date-info, .time-info {
  margin-right: 15px;
}

.plan-remark {
  background-color: #f5f7fa;
  padding: 8px;
  border-radius: 4px;
  font-size: 13px;
  color: #606266;
  margin-top: 8px;
}

.remark-label {
  color: #909399;
  margin-bottom: 4px;
}

.remark-content {
  line-height: 1.5;
}

.card-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  margin-left: 20px;
}

.status-tag {
  margin-bottom: 10px;
}

.detail-btn {
  min-width: 90px;
}

.pagination-container {
  margin-top: 30px;
  text-align: center;
}

@media (max-width: 768px) {
  .check-in-record {
    padding: 15px;
  }
  
  .query-section {
    padding: 15px;
  }
  
  .card-content {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .card-left {
    margin-right: 0;
    margin-bottom: 10px;
  }
  
  .card-right {
    margin-left: 0;
    margin-top: 15px;
    flex-direction: row;
    justify-content: space-between;
    width: 100%;
  }
  
  .status-tag {
    margin-bottom: 0;
  }
  
  .plan-title {
    width: 100%;
    margin-bottom: 10px;
    margin-right: 0;
  }
}
</style>